/**
 * Created by Mark_ren on 2016/11/7.
 * 苟富贵,無相忘
 */

var express = require('express');
var router = express.Router();
var co = require('co');
var seqModule = require('./createModules');
var seq = new seqModule();
var request = require('request');
var util = require('./util');

/**
 * 查询方法
 */
router.get('/getMemberCardPage', function (req, res, next) {
    co(function() {
        var page = parseInt(req.query.page);
        var rows = parseInt(req.query.rows);
        var memCardCount = "select count(*) as total from member_card as memCard left join member as mem on memCard.member_id = mem.id where 1=1 ";
        var memberCardSql = "select memCard.card,memCard.member_id,memCard.type,mem.name,mem.tel,mem.hospital_name,mem.hospital_offices,mem.job_title,mem.filed9 as checkState,mem.filed7 as cause,mem.open_id as openId,mem.filed10  from "+
            " member_card as memCard left join member as mem on memCard.member_id = mem.id where 1=1 ";
        if(req.query.name != undefined){
            memberCardSql+=" and mem.name like '%"+req.query.name+"%'";
            memCardCount+=" and mem.name like '%"+req.query.name+"%'";
        }

        if(req.query.tel != undefined){
            memberCardSql+=" and mem.tel like '%"+req.query.tel+"%'";
            memCardCount+=" and mem.tel like '%"+req.query.tel+"%'";
        }

        if(req.query.deleteState != undefined){
            memberCardSql+=" and memCard.delete_state="+parseInt(req.query.deleteState);
            memCardCount+=" and memCard.delete_state="+parseInt(req.query.deleteState);
        }
        if(req.query.type != undefined){
            memberCardSql+=" and memCard.type="+parseInt(req.query.type);
            memCardCount+=" and memCard.type="+parseInt(req.query.type);
        }
        memberCardSql+=" order by memCard.create_time DESC";

        if(req.query.page != undefined && req.query.rows !=undefined){
            memberCardSql+=" limit "+page+","+rows+"";
        }

        seq.sequelize.query(memCardCount).spread(function(resCount){
            seq.sequelize.query(memberCardSql).spread(function(result){
                res.json({"success":true,"page":page,"total":resCount[0].total,"result":result});
            });
        })

    }).catch(function (e) {
        res.json({'success': false, 'msg': '查询失败'});
        console.log(e);
    })


});

//
router.get('/updtMemCheck', function(req, res, next) {
    co(function () {
        var params = {};
        var paramT = {};
        var paramU = {};
        var sqlRecord = "";
        var memberId = req.query.id;
        paramU.id=memberId;
        var openId = req.query.openId;
        var filed9 = req.query.filed9;
        params.filed9=filed9;
        var contentMsg="";
        if(filed9 == "1"){
            contentMsg="恭喜您，医师资格证书认证通过！获得"+util.getCheck()+"积分！";
            var filed10 = parseInt(req.query.filed10);
            filed10+=parseInt(util.getCheck());
            params.filed10=filed10+"";

            sqlRecord+="insert into integral_record values ('"+util.getUUID()+"','"+memberId+"',2,1,'"+util.getCheck()+"','"+util.getNowDateTime()+"','','"+openId+"','','','')";

        }else if(filed9 == "2"){
            var filed7 = req.query.filed7;
            params.filed7=filed7;
            contentMsg = "您的医师资格证书认证未通过，原因："+filed7;
        }else{
            console.log("filed9=========="+filed9);
            return false;
        }
        paramT.where = paramU;
        seq.memberModule.update(params,paramT).then(function(results){
            if(sqlRecord != ""){
                seq.sequelize.query(sqlRecord).spread(function(data){
                    console.log(data);
                })
            }
            request(util.getTomIP()+"nodeJs.action?sendWxInfo&openId="+openId+"&contentMsg="+encodeURI(contentMsg), function (error, response, body) {
                if (!error && response.statusCode == 200) {

                    res.json({"success":true,"msg":"修改成功","result":body});
                }else{
                    res.json({"success":false,"msg":"修改失败","result":body});
                }
            })
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'修改失败'});
        console.log(e);
    })
});

module.exports = router;